<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta charset="UTF-8">
	<title>编辑抵扣卡</title>
	<meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />

    
<!-- 最新导入 -->
<script type="text/javascript" src="<c:url value='/template/new_muban/js/jquery-min-1.7.js'/>"></script>
<link rel="stylesheet" href="<c:url value='/template/new_muban/css/font.css'/>">
<link rel="stylesheet" href="<c:url value='/template/new_muban/css/xadmin.css'/>">   
<script src="<c:url value='/template/new_muban/lib/layui/layui.js'/>" charset="utf-8"></script> 
<script type="text/javascript" src="<c:url value='/template/new_muban/js/xadmin.js'/>"></script>

 <!-- 富文本编辑器 -->
<script type="text/javascript" charset="utf-8" src="<c:url value='/ue/ueditor.config.js'/>"></script>
<script type="text/javascript" charset="utf-8" src="<c:url value='/ue/ueditor.all.js'/>"> </script>



<!-- 标签 -->
<link rel="stylesheet" type="text/css" href="<c:url value='/template/lead_into/tags_input/css.css'/>" />
<script src="<c:url value='/template/lead_into/tags_input/tagsinput.js'/>" type="text/javascript" charset="utf-8"></script>
		
	
<style type="text/css">
	/*圆形头像  */
	/* #divcss5{ margin:10px auto}  */
	#divcss5 img{ border-radius:50%} 
	.x-so input.layui-input {
    	width: 150px;
	}
</style>
<style>
.layui-upload-img {
	width: 92px;
	height: 92px;
	margin: 0 10px 10px 0;
}
</style>
<style type="text/css">
.uploader-list {
	margin-left: -15px;
}

.uploader-list .info {
	position: relative;
	margin-top: -25px;
	background-color: black;
	color: white;
	filter: alpha(Opacity = 80);
	-moz-opacity: 0.5;
	opacity: 0.5;
	width: 100px;
	height: 25px;
	text-align: center;
	display: none;
}

.uploader-list .handle {
	position: relative;
	background-color: black;
	color: white;
	filter: alpha(Opacity = 80);
	-moz-opacity: 0.5;
	opacity: 0.5;
	width: 100px;
	text-align: right;
	height: 18px;
	margin-bottom: -18px;
	display: none;
}

.uploader-list .handle i {
	margin-right: 5px;
}

.uploader-list .handle i:hover {
	cursor: pointer;
}

.uploader-list .file-iteme {
	margin: 12px 0 0 15px;
	padding: 1px;
	float: left;
}

.input-50{
    width: 50%;
}
</style>
</head>
<body>
   
     <div class="x-body">
        <form class="layui-form ">
          
           <input type="hidden" id="c_id" value="${cardDetails.c_id}">
         
          
          <div class="layui-form-item">
              <label for="username" class="layui-form-label" >
                  <span class="x-red">*</span>抵扣卡名称
              </label>
              <div class="layui-input-inline">
                    <input type="text" id="c_title" name="c_title" placeholder="请输入抵扣卡名称" lay-verify="required"
                  autocomplete="off" class="layui-input" value="${cardDetails.c_title}">
              </div>
          </div>
          
           <!-- <div class="layui-form-item" >
		    <label for="L_username" class="layui-form-label">
		    <span class="x-red">*</span>标签设置
		    </label>
		    <div class="layui-input-block">
					 <div class="tagsinput-primary form-group">
			                  <input name="tagsinput" id="tagsinputval" lay-verify="required"
			                  		class="tagsinput" data-role="tagsinput" value="随买随用,特惠,不可退款新增优惠券" placeholder="输入后回车" />
			             
		              </div>
		    </div>
		  </div> -->
          
  		<div class="layui-form-item">
              <label for="username" class="layui-form-label" >
                  <span class="x-red">*</span>抵扣值(百分制)
              </label>
              <div class="layui-input-inline">
                    <input type="username" id="c_price"  lay-verify="required" value="${cardDetails.c_price}"
                  autocomplete="off" class="layui-input" placeholder="请输入折扣,最大值100即不打折,最小值10即1折"    onkeyup="this.value=this.value.replace(/\D/g,'');">
              </div>
          </div>
         <%--  <div class="layui-form-item">
              <label for="username" class="layui-form-label" >
                  <span class="x-red">*</span>优惠券使用价格
              </label>
              <div class="layui-input-inline">
                    <input type="username" id="c_pay_money"  lay-verify="required" value="${couponDetails.c_pay_money}" 
                  autocomplete="off" class="layui-input" placeholder="请输入优惠券使用限制价格"  onkeyup="clearNoNum(this)">
              </div>
          </div> --%>
          
          <div class="layui-form-item">
              <label for="username" class="layui-form-label">
                  <span class="x-red">*</span>开始时间
              </label>
              <div class="layui-input-inline"  >
                 <input class="layui-input"  lay-verify="required" value="${cardDetails.c_btime}"  placeholder="开始日" name="start" id="start" readonly>
              </div>
          </div>
          <div class="layui-form-item">
              <label for="username" class="layui-form-label">
                  <span class="x-red">*</span>到期时间
              </label>
              <div class="layui-input-inline"  >
                 <input class="layui-input" lay-verify="required" value="${cardDetails.c_etime}"   placeholder="截止日" name="end" id="end" readonly>
              </div>
          </div>
         <%--  <div class="layui-form-item">
              <label for="username" class="layui-form-label" >
           <span class="x-red">*</span>每人限领数量
              </label>
              <div class="layui-input-inline">
                    <input type="username" id="c_xz_count"  value="${couponDetails.c_xz_count}" 
                  autocomplete="off" lay-verify="required" class="layui-input" placeholder="请输入每人最高领取的数量"   onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">
              </div>
          </div>
      
          <div class="layui-form-item">
              <label for="username" class="layui-form-label" >
                 优惠券数量
              </label>
              <div class="layui-input-inline">
                    
             	<c:if test="${couponDetails.c_counts == -1}">
               		<input type="username" id="c_counts" autocomplete="off" class="layui-input" placeholder="无限" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">
                </c:if>
                <c:if test="${couponDetails.c_counts != -1}">
               		<input type="username" id="c_counts" autocomplete="off" value="${couponDetails.c_counts}" class="layui-input" placeholder="不设置默认为无限" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">
                </c:if>
             
             
              </div>
          </div> --%>
          
          
          
                 <div class="layui-form-item">
			    <label class="layui-form-label">是否指定门票使用</label>
			    <div class="layui-input-block">
			      <input type="checkbox" <c:if test="${cardDetails.c_type == 1}">checked=""</c:if> name="c_type" lay-skin="switch" lay-filter="power1" lay-text="ON|OFF">
			    </div>
  		  </div>
          
          
           <div class="layui-form-item layui-row">
              <label for="username" class="layui-form-label" >
         指定门票
              </label>
              <div class="layui-input-inline" style="width:50%">
              	<c:forEach	items="${allPromotionByFlagAndStatus}" var="pf">
              		
                    <input type="checkbox" name="t_id" value="${pf.t_id}" title="${pf.t_name}" lay-skin="primary" 
                   		<c:forEach	items="${cardDetails.c_ticket_list}" var="c_ticket">
                   			<c:if test="${pf.t_id == c_ticket}">checked=""</c:if>
                   		</c:forEach> 
                    >
              		<div class="layui-unselect layui-form-checkbox" lay-skin="primary"><span>${pf.t_name}</span><i class="layui-icon layui-icon-ok"></i></div>
              		
              	</c:forEach>
              </div>
          </div> 
          
          
          <div class="layui-form-item layui-form-text">
			<label for="desc" class="layui-form-label"> <span
				class="x-red">*</span>图片
			</label>
			<div class="layui-input-inline">
			<input type="hidden" id="c_img" lay-verify="required" value="${cardDetails.c_img}"> 
				<button type="button" class="layui-btn" id="c_img_btn">上传图片</button>
				<div class="layui-upload-list">
					<img class="layui-upload-img" id="c_img_demo" src="${cardDetails.c_img}">
					<p id="c_img_demoText"></p>
				</div>
			</div>
		</div>
          
          
           <div class="layui-form-item layui-form-text">
              <label for="desc" class="layui-form-label">
           <span class="x-red">*</span> 购票须知
              </label>
              <div class="layui-input-block" >
               	<script id="editor" type="text/plain" style="width:100%;height:500px;">${cardDetails.c_info}</script>
              </div>
           </div> 
          	  
          
        <%--   <div class="layui-form-item">
              <label for="username" class="layui-form-label" >
                排序
              </label>
              <div class="layui-input-inline">
                    <input type="username" id="c_num" value="${couponDetails.c_num}" 
                  autocomplete="off" class="layui-input" placeholder="不设置默认为0"   onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">
              </div>
          </div> --%>
          
        <!--   <div class="layui-form-item layui-form-text">
              <label for="desc" class="layui-form-label">
           <span class="x-red">*</span> 购票须知
              </label>
              <div class="layui-input-block" >
               	<script id="editor" type="text/plain" style="width:100%;height:500px;"></script>
              </div>
           </div> -->
          	
         
          <div class="layui-form-item" id="upload_show" ><!--  style="display: none;" -->
              <label for="L_repass" class="layui-form-label">
              </label>
              <button  class="layui-btn" lay-filter="add" lay-submit="">
                修改
              </button>
          </div>
      </form>
    </div>
    <script>
    

    //时间
    layui.use('laydate', function(){
      var laydate = layui.laydate;
      
      //执行一个laydate实例
      laydate.render({
        elem: '#start' //指定元素
        	,type: 'datetime'
      });

      //执行一个laydate实例
      laydate.render({
        elem: '#end' //指定元素
        	,type: 'datetime'
      });
    });
  
    
        layui.use(['form','layer'], function(){
            $ = layui.jquery;
          var form = layui.form
          ,layer = layui.layer;
        
          
  /*         var cond = true;
          //监听switch开关
        form.on('switch(power)', function(data){
    	 // console.log(data.elem); //得到checkbox原始DOM对象
    	  console.log(data.elem.checked); //开关是否开启，true或者false
    	 // console.log(data.value); //开关value值，也可以通过data.elem.value得到
    	 // console.log(data.othis); //得到美化后的DOM对象
    	 	cond = data.elem.checked;
    	 //	alert(cond);
    	 	
    	 });   */
          
    	 var c_type = "${cardDetails.c_type}";
    	 
         var cond1 = false;
    	 if(c_type == 0){
    		 cond1 = false;
    	 }else{
    		 cond1 = true;
    	 }
         //监听switch开关
       form.on('switch(power1)', function(data){
   	 // console.log(data.elem); //得到checkbox原始DOM对象
   	  console.log(data.elem.checked); //开关是否开启，true或者false
   	 // console.log(data.value); //开关value值，也可以通过data.elem.value得到
   	 // console.log(data.othis); //得到美化后的DOM对象
   	 	cond1 = data.elem.checked;
   	 //	alert(cond);
   	 	
   	 });   
          
          
          //监听提交
          form.on('submit(add)', function(data){
        	  
            console.log(data);
            
          //优惠券名称
          var c_title = $("#c_title").val();
       //   alert("图片路径     :  "+file_url);
          //优惠券价格
          var c_price = $("#c_price").val();
        //  alert("小程序路径    :  "+ xcx_lication);
          //优惠券使用条件价格
       //   var c_pay_money = $("#c_pay_money").val();
          //开始时间
          var c_btime = $("#start").val();        
        //到期时间
          var c_etime = $("#end").val();
          //优惠券数量
        //  var c_counts = $("#c_counts").val();
          //每人限领次数
        //  var c_xz_count = $("#c_xz_count").val();
          //排序
      //    var c_num = $("#c_num").val();
          
          //id
            var c_id= $("#c_id").val();
         
       
      /*   //优惠券状态
        var c_flag = 0;
        //是否启用
        if(cond == false){
        	c_flag = 1;
        }*/
        
       // if(c_counts == ''){
       // 	c_counts = -1;
      //  } 

       // if(c_num == ''){
       // 	c_num = 0;
       // }
        
        //时间判断
        if(c_btime > c_etime){
        	layer.msg('结束日期不能小于开始日期!',{icon: 2,time:3000});      
            return false;
        }
            
        
        

        
        var c_ticket_list = $("input:checkbox[name='t_id']:checked").map(function(index,elem) {
            return $(elem).val();
        }).get().join(',');
        
        //是否启用
        if(cond1 == true){
        	c_type = 1;
        }else{
        	c_type = 0;
        	
        }
        
        if(c_type == 0){
        	//清空
        	c_ticket_list = "";
        }
        
        if(c_ticket_list == null || c_ticket_list == ""){
        	c_type = 0;
        }
        
        console.log(c_ticket_list);
        console.log(c_type);
   var c_img =  $("#c_img").val();
        
        var c_info = UE.getEditor('editor').getContent();
        if(c_info == "" || c_info == null){
       	  layer.msg('请输入优惠券说明!',{icon: 2,time:3000});      
	      	  return false; 
        } 
   
          var aa = 0;
          //发异步，把数据提交给php
           $.ajax({
               type: "POST",
               async : false, // 注意此处需要yi步  
               url:  "<c:url value='/updateCardDetails.do'/>",
               data: {"c_id":c_id,"c_title":c_title,"c_price":c_price,"c_btime":c_btime,"c_etime":c_etime
            		,"c_ticket_list":c_ticket_list,"c_type":c_type,"c_img":c_img,"c_info":c_info      
               },
               success: function(data){
                   if (data == 0) {
                  	 aa = 0;
                   } else  {
                  	 aa = 1;
		           } 
              }
	        });   
          
           if(aa == 0){
               layer.msg('修改失败,未知错误!',{icon: 2,time:3000});      
               return false;
           }else if(aa == 1){
           	
          	  layer.msg("修改成功", {icon: 6,time:500},function () {
                    // 获得frame索引
                    var index = parent.layer.getFrameIndex(window.name);
                    //alert(index);
                    //关闭当前frame
                    parent.layer.close(index);
                    //重新加载父页面
                    parent.location.reload();
                });
           }
         	//禁止页面自动跳转
            return false;
          });
        });
    </script>
    
    
   <script>
		
  layui.use('upload', function(){
  	  var $ = layui.jquery
  	  ,upload = layui.upload;
	
	/* 单图  */
	var uploadInst = upload.render({
		    elem: '#c_img_btn'
	 	   ,url: '<%=request.getContextPath()%>/uploadToQiNiu.do'
	 	   ,auto:true
	 	,size: 1024//限制文件大小，单位 KB
	 	   ,exts: 'jpg|png'
	 	   ,xhr:xhrOnProgress
	 	   ,progress:function(value){//上传进度回调 value进度值
	 	     console.log(value);
	 	     element.progress('demo', value+'%')//设置页面进度条
	 	    }
	 	   //,bindAction: '#test-upload-change-action'
	 	   ,before: function(obj){
	 	   //返回的参数item，即为当前的input DOM对象
	 	     layer.load(); //上传loading
	 	       console.log('文件上传中');
	 	 
	 	     //预读本地文件示例，不支持ie8
	 	     obj.preview(function(index, file, result){
	 	       $('#c_img_demo').attr('src', result); //图片链接（base64）
	 	     });
	 	 
	 	   }
	 	   ,done: function(res){
	 	     console.log("返回码 : "+res.code);
	 		
	  		 //如果上传失败
	 	      if(res.code != 200){
	 	        return layer.msg('上传失败');
	 	      }
	 	     layer.closeAll();
	 	     layer.msg("上传成功");
	 	      //上传成功
	 	      $("#c_img").val(res.file_url);
	 		
	 	   },error: function(){
		      //演示失败状态，并实现重传
		      var demoText = $('#c_img_demoText');
		      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
		      demoText.find('.demo-reload').on('click', function(){
		        uploadInst.upload();
		      });
		    }

	}); 
    
  
    }); 
   
	
	
	//该部分代码主要使用ajax的xhr属性，我copy别人的，自己也不会写，应该不用管。

	    var xhrOnProgress=function(fun) {
	       xhrOnProgress.onprogress = fun; //绑定监听
	        //使用闭包实现监听绑
	       return function() {
	           //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
	           var xhr = $.ajaxSettings.xhr();
	            //判断监听函数是否为函数
	             if (typeof xhrOnProgress.onprogress !== 'function')
	                  return xhr;
	              //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
	               if (xhrOnProgress.onprogress && xhr.upload) {
	                     xhr.upload.onprogress = xhrOnProgress.onprogress;
	               }
	               return xhr;
	        }
	    };
  </script>
  <!-- 标签input -->
<script type="text/javascript">
function getinput(){
	alert($('#tagsinputval').val());
}     
</script>    
<!-- 小数验证 -->
<script type="text/javascript">
//小数验证
function clearNoNum(obj){  
    //修复第一个字符是小数点 的情况.  
    if(obj.value !=''&& obj.value.substr(0,1) == '.'){  
        obj.value="";  
    }  
    obj.value = obj.value.replace(/^0*(0\.|[1-9])/, '$1');//解决 粘贴不生效  
    obj.value = obj.value.replace(/[^\d.]/g,"");  //清除“数字”和“.”以外的字符  
    obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的       
    obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");      
    obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');//只能输入两个小数       
    if(obj.value.indexOf(".")< 0 && obj.value !=""){//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额  
        if(obj.value.substr(0,1) == '0' && obj.value.length == 2){  
            obj.value= obj.value.substr(1,obj.value.length);      
        }  
    }      
}      
  

</script>
<!-- ue初始化 -->
<script type="text/javascript">
var ue = UE.getEditor('editor',{
	autoFloatEnabled:true,//浮动
	toolbars: [[
		'source','bold', 'italic',  'removeformat', 'formatmatch', 'autotypeset','selectall', 'cleardoc', 
       'lineheight', '|','fontfamily', 'fontsize', 'forecolor', 'backcolor','|',
        'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|','insertimage', 'emotion', 
    ]]
});
</script>

      
  </body>
</html>